import React from 'react';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';
import {conventEnumValueToString, formatAmount, formatDateTime} from '../utils';
import {transportTypeEng, currency, partOrderPriceTerms, partShipmentStatus, partShipNoticeStatus, partShipOrderStatus, partContractInvoiceStatus} from '../Enum';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';

export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const {brands} = this.props.initEnumData;
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.partInvoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'partInvoiceCode',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'partInvoiceCode' && this.props.sortedOrder,
            render: (text, record) => <Link key="detail" to={routes.detail.format(record.partInvoiceId)}>{text}</Link>
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.status',
                defaultMessage: '发票状态'
            }),
            dataIndex: 'status',
            render: text => conventEnumValueToString(partContractInvoiceStatus, text),
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'status' && this.props.sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.createTime',
                defaultMessage: '发票生成日期'
            }),
            dataIndex: 'createTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partSaleOrderCode',
                defaultMessage: '订单号'
            }),
            dataIndex: 'partSaleOrderCode',
            render: text => <Ellipsis tooltip fullWidthRecognition length={20}>{text}</Ellipsis>
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.priceTerms',
                defaultMessage: '价格条款'
            }),
            dataIndex: 'priceTerms',
            render: text => conventEnumValueToString(partOrderPriceTerms, text),
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'priceTerms' && this.props.sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalAmount',
                defaultMessage: 'FOB金额'
            }),
            dataIndex: 'totalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partShipmentCode',
                defaultMessage: '发运单'
            }),
            dataIndex: 'partShipmentCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalQuantity',
                defaultMessage: '发运数量'
            }),
            dataIndex: 'totalQuantity'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.invoiceAmount',
                defaultMessage: '发票总金额'
            }),
            dataIndex: 'invoiceAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.freight',
                defaultMessage: '运费'
            }),
            dataIndex: 'freight',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.insurance',
                defaultMessage: '保费'
            }),
            dataIndex: 'insurance',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalDiscount',
                defaultMessage: '折扣总金额'
            }),
            dataIndex: 'totalDiscount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.currency',
                defaultMessage: '币种'
            }),
            dataIndex: 'currency',
            render: text => conventEnumValueToString(currency, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shipmentType',
                defaultMessage: '发运方式'
            }),
            dataIndex: 'shipmentType',
            render: text => conventEnumValueToString(transportTypeEng, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partShipOrderStatus',
                defaultMessage: '委托书状态'
            }),
            dataIndex: 'partShipOrderStatus',
            render: text => conventEnumValueToString(partShipOrderStatus, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partShipNoticeStatus',
                defaultMessage: '通知书状态'
            }),
            dataIndex: 'partShipNoticeStatus',
            render: text => conventEnumValueToString(partShipNoticeStatus, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partShipmentStatus',
                defaultMessage: '运单状态'
            }),
            dataIndex: 'partShipmentStatus',
            render: text => conventEnumValueToString(partShipmentStatus, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.expressCompanyCode',
                defaultMessage: '快递公司'
            }),
            dataIndex: 'expressCompanyCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.expectDepartedDate',
                defaultMessage: '预计离港日期'
            }),
            dataIndex: 'expectDepartedDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.expectArrivalDate',
                defaultMessage: '预计到港日期'
            }),
            dataIndex: 'expectArrivalDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.actualDepartedDate',
                defaultMessage: '实际离港日期'
            }),
            dataIndex: 'actualDepartedDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.actualArrivalDate',
                defaultMessage: '实际到港日期'
            }),
            dataIndex: 'actualArrivalDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.receiptTime',
                defaultMessage: '签收日期'
            }),
            dataIndex: 'receiptTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shortContainerCount',
                defaultMessage: '20GP*数量'
            }),
            dataIndex: 'shortContainerCount'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.standardContainerCount',
                defaultMessage: '40GP*数量'
            }),
            dataIndex: 'standardContainerCount'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.highContainerCount',
                defaultMessage: '40HQ*数量'
            }),
            dataIndex: 'highContainerCount'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.carrierName',
                defaultMessage: '货代'
            }),
            dataIndex: 'carrierName'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shippingCompanyAirline',
                defaultMessage: '船公司/航空公司'
            }),
            dataIndex: 'shippingCompanyAirline'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.outboundDate',
                defaultMessage: '出库日期'
            }),
            dataIndex: 'outboundDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.postDate',
                defaultMessage: '发票上传日期	'
            }),
            dataIndex: 'postDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }];
        if(brands.length !== 1)
            columns.splice(0, 0, ...[
                {
                    title: formatMessage({
                        id: 'tablePanel.column.brandName',
                        defaultMessage: '品牌'
                    }),
                    dataIndex: 'brandCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'brandCode' && this.props.sortedOrder,
                    render: (text, r) => `(${r.brandCode})${r.brandName}`
                }
            ]);
        if(!this.props.isDealerUser)
            columns.splice(0, 0, ...[
                {
                    title: formatMessage({
                        id: 'tablePanel.column.marketDepartmentName',
                        defaultMessage: '大区'
                    }),
                    dataIndex: 'marketDepartmentName',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'marketDepartmentName' && this.props.sortedOrder,
                }, {
                    title: formatMessage({
                        id: 'tablePanel.column.country',
                        defaultMessage: '国家'
                    }),
                    dataIndex: 'country',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'country' && this.props.sortedOrder,
                }, {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.dealerCode',
                        defaultMessage: '总代ERP号'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
                }
            ]);
        return (
            <Card>
                <Table
                    className={'white-space-nowrap'}
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={pagination}
                    rowKey="id"
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
import {injectIntl} from './intl';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    initEnumData: getinitEnumData(state),
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
